package com.caocaog.market.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caocaog.market.finance.entity.FinanceGroupLineMerge;
import com.caocaog.market.finance.mapper.FinanceGroupLineMergeMapper;
import com.caocaog.market.finance.service.FinanceGroupLineMergeService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FinanceGroupLineMergeImpl extends ServiceImpl<FinanceGroupLineMergeMapper, FinanceGroupLineMerge> implements FinanceGroupLineMergeService {

    @Override
    public List<Long> getLineIds(Long groupId) {
        return baseMapper.selectList(new LambdaQueryWrapper<FinanceGroupLineMerge>()
                        .eq(FinanceGroupLineMerge::getGroupId, groupId))
                .stream()
                .map(FinanceGroupLineMerge::getLineId)
                .collect(Collectors.toList());
    }


    @Override
    public Boolean mergeFinanceGroupLine(Long groupId, List<Long> lineIds) {
        List<FinanceGroupLineMerge> financeGroupLineMerges = lineIds.stream().map(lineId ->
                new FinanceGroupLineMerge()
                        .setGroupId(groupId)
                        .setLineId(lineId)
        ).collect(Collectors.toList());

        return saveBatch(financeGroupLineMerges);
    }


    @Override
    public void deleteFinanceGroupLineMergeByGroupId(Long groupId) {
        baseMapper.delete(new LambdaQueryWrapper<FinanceGroupLineMerge>()
                .eq(FinanceGroupLineMerge::getGroupId, groupId));
    }


    @Override
    public void deleteFinanceGroupLineMergeByLineIds(Long... lineIds) {
        baseMapper.delete(new LambdaQueryWrapper<FinanceGroupLineMerge>()
                .in(FinanceGroupLineMerge::getLineId, (Object[]) lineIds));
    }

}
